package com.fatiny.core.proto;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * 协议结构
 */
public class ProtocolStructure {
	/**
	 * 协议号
	 */
	private int protoId;
	/**
	 * 协议名称
	 */
	private String name;
	/**
	 * 备注信息
	 */
	private String comment;
	/**
	 * 字段属性
	 */
	private List<ProtocolField> fields = new ArrayList<>();
	/**
	 * 要生成的协议class名
	 */
	private String baseName;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * 获取协议类名
	 * @return
	 */
	public String getBaseName() {
		if (StringUtils.isBlank(baseName)){//名字为空
			int index = name.lastIndexOf("Proto");
			this.baseName = name.substring(0, index);
		}
		return this.baseName;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = comment;
		if (comment.contains("msgId")) {
			int idx = comment.indexOf("=")+1;
			String protoIdStr = comment.substring(idx, comment.length());
			this.protoId = Integer.parseInt(protoIdStr);
			this.comment = comment.substring(0, comment.indexOf("m")-1);
		}
	}
	public List<ProtocolField> getFields() {
		return fields;
	}
	public int getProtoId() {
		return protoId;
	}
	@Override
	public String toString() {
		return "ProtocolStructure [protoId=" + protoId + ", name=" + name + ", comment=" + comment + ", fields="
				+ fields + ", baseName=" + baseName + "]";
	}
	
}
